﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Diagnostics;

using LowLevelGraphics.Filter;

namespace LowLevelGraphics
{
    /// <summary>
    /// 
    /// </summary>
    public class NeighbourFunctions
    {
        private UnsafeBitmap m_Bitmap = null;

        /// <summary>
        /// Initializes a new instance of the <see cref="NeighbourFunctions"/> class.
        /// </summary>
        /// <param name="_bitmap">The _bitmap.</param>
        public NeighbourFunctions(UnsafeBitmap _bitmap)
        {
            m_Bitmap = _bitmap;
        }

        //TODO: NeighbourFunctions, Test Create Function
        /// <summary>
        /// To use this, clear a bitmap (e.g. backgrund is completely black)
        /// then draw a white circle (radian 3) using normal gdi functions (center of circle is _nX, _nY)
        /// Then use this functions (unoptimized):
        /// 
        /// Create(Color.White, Color.Black, _nX, _nY, new Rectangle(0,0, m_Bitmap.Width, m_Bitmap.Height))
        /// 
        /// If you'd like to do this optimized then you should know the spanning rect of the circle,
        /// and it is for sure a much faster generation of the neighbouring offsets:
        /// 
        /// Create(Color.White, Color.Black, _nX, _nY, new Rectangle(_nX - 3,_nY - 3, _nX + 3, _nY + 3))
        /// 
        /// 
        /// 
        /// </summary>
        /// <param name="_colorNeighbour"></param>
        /// <param name="_colorBack"></param>
        /// <param name="_nX"></param>
        /// <param name="_nY"></param>
        /// <param name="_rectROI"></param>
        /// <returns></returns>
        public Neighbours Create(Color _colorNeighbour, Color _colorBack, int _nX, int _nY, Rectangle _rectROI)
        {
            Neighbours aNeighbour = new Neighbours();

            int nX1 = _rectROI.Left;
            int nX2 = _rectROI.Right;
            int nY1 = _rectROI.Top;
            int nY2 = _rectROI.Bottom;

            for (int y = nY1; y < nY2; y++)
            {
                for (int x = nX1; x < nX2; x++)
                {
                    _colorNeighbour = m_Bitmap.GetPixel(x, y);
                    if (_colorNeighbour != _colorBack)
                    {
                        aNeighbour.Add(new Neighbour(_nX - x, _nY - y));
                    }
                }
            }
            return aNeighbour;
        }
    }
}
